import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;
import org.wolfengi.handler.common.ShipCN;
import org.wolfengi.handler.util.HDFSUtils;
import org.wolfengi.handler.util.StringTools;

import java.io.*;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: wolfengi
 * @Description: ${TODO}
 * @Date: Created in 16:41 2020/5/18
 * @Modified By:
 */
public class T01 {
    @Test
    public void t1() throws UnsupportedEncodingException {
//        System.out.println(HDFSUtils.deleteRes(Constants.ouputPath));

//        String str = "锟斤拷锟斤拷山锟斤拷群锟斤拷锟叫帮拷锟斤拷诺锟斤拷";
//        String str = "阿历山大群岛中巴拉诺夫";
////        String str = "����ɽ��Ⱥ���а���ŵ��";
//        String s = new String(str.getBytes("GBK"), "UTF-8");
//        System.out.println(s);
        FileSystem fs = HDFSUtils.getFileSystem();
        String url = "/mr/data/dict.txt";
        String source = "E:/wolfengi/dict.txt";
        try {
            InputStreamReader ins = new InputStreamReader(new FileInputStream(source), "GBK");
            BufferedReader br = new BufferedReader(ins);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                // 注意写入换行符
                sb.append(line + "\n");
            }
            ByteArrayInputStream bin = new ByteArrayInputStream(sb.toString().getBytes());

            Path path = new Path(url);
            OutputStream out = fs.create(path);
            IOUtils.copyBytes(bin, out, 4096, true);
            System.out.println("上传成功！");
            out.close();


            InputStream inp = fs.open(path);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            int ch;
            while ((ch = inp.read()) != -1) {
                os.write(ch);
            }

            System.out.println(os.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t2() {
        DateTimeFormatter dt = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm");
        DateTime dateTime = DateTime.parse("2018/1/6 7:20", dt);
        System.out.println(dateTime.toString("YYYY-MM-dd HH:mm"));
    }


    @Test
    public void t3() throws IOException {
        String source = "E:/wolfengi/dict.txt";
        InputStreamReader ins = new InputStreamReader(new FileInputStream(source), "GB2312");
        BufferedReader br = new BufferedReader(ins);
        StringBuffer sb = new StringBuffer();
        String line = null;
        while ((line = br.readLine()) != null) {
            // 注意写入换行符
            sb.append(line + "\n");
        }
        br.close();
        ins.close();
        System.out.println(sb.toString());

//        InputStreamReader ins = new InputStreamReader(new FileInputStream(source), "GBK");
//        BufferedReader br = new BufferedReader(ins);
//
//        FileOutputStream fos = new FileOutputStream("E:/wolfengi/data1.txt");
//        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
//        BufferedWriter bw = new BufferedWriter(osw);
//        String str;
//        while ((str = br.readLine()) != null) {
//            bw.write(str);
//            bw.newLine();
//            bw.flush();
//        }
//        br.close();
//        bw.close();
    }


    @Test
    public void t4() throws Exception {
        String s = "a  b       c   d";
        String[] split = s.split("\t");
        List<String> list = Arrays.asList(split);

        InputStreamReader in = new InputStreamReader(new FileInputStream("E:/wolfengi/dict.txt"));
        BufferedReader br = new BufferedReader(in);
        StringBuffer sbf = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            String line1 = line.replaceAll("\"", "");
            if (isNONE(line1)) {
                System.out.println(line1);
            }
        }

        System.out.println();
    }


    private static boolean isNONE(String columns) {
        String[] split = columns.split("\t");
        /**
         * TODO: 字典表：当 column 的数量为4时，说明数据正常，进行处理
         */
        if (split.length == 4) {
            for (String s : Arrays.asList(split)) {
                if (StringTools.isNull(s)) {
                    /**
                     * 为空时，返回true，说明数据不对，会进入上层if条件，不通过后面清洗步骤
                     */
                    return true;
                }
            }
            if (null == ShipCN.getCNTrim(split[split.length - 1].trim())) {
                return true;
            }
            return false;
        }
        /**
         * TODO: 数据表：当 column 的数量为4时，说明数据正常，进行处理
         */
        if (split.length == 18) {
            for (String s : Arrays.asList(split)) {
                if (StringTools.isNull(s)) {
                    /**
                     * 为空时，返回true，说明数据不对，会进入上层if条件，不通过后面清洗步骤
                     */
                    return true;
                }
            }
            if (null == ShipCN.getCNTrim(split[split.length - 1].trim()) || null == ShipCN.getCNTrim(split[split.length - 6].trim())) {
                return true;
            }
            return false;
        }
        /**
         * TODO：当
         *  文件名不是 dict.txt & data.txt
         *  文件内容包含空格，空字符串
         *  状态 和 故障类型 在该对应的类型范围之外
         *  返回 true
         */
        return true;
    }

}
